<template>
  <div>
    <editCourse :organization-id="organizationId" @release="release" @addDraft="addDraft" />
  </div>
</template>

<script>
import editCourse from '@/views/course/curriculum/compoents/editCourse'
import { addCourse } from '@/api/curriculum'
export default {
  name: 'CourseAdd',
  components: {
    editCourse
  },
  data() {
    return {
      add: false,
      courseLabel: [],
      organizationId: ''
    }
  },
  beforeRouteLeave(to, form, next) {
    if (!this.add) {
      this.$confirm('离开当前页面编辑内容将会被清空, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('tagsView/delView', this.$route)
        next()
      }).catch(() => {
        next(false)
      })
    } else {
      next()
    }
  },
  created() {
    this.organizationId = this.$route.query.id
  },
  methods: {
    release(form) {
      const data = Object.assign({}, form)
      data.status = 2
      data.businessId = this.organizationId
      addCourse(data).then(res => {
        this.$message({
          message: '新增成功',
          type: 'success'
        })
        this.add = true
        this.$store.dispatch('tagsView/delView', this.$route)
        // this.$router.push('/course/curriculum/courseList')
        this.$router.go(-1)
      })
    },
    addDraft(form) {
      const data = Object.assign({}, form)
      data.status = 1
      data.businessId = this.$store.getters.businessId
      addCourse(data).then(res => {
        this.$message({
          message: '保存草稿成功',
          type: 'success'
        })
        this.add = true
        this.$store.dispatch('tagsView/delView', this.$route)
        // this.$router.push('/course/curriculum/draft')
        this.$router.go(-1)
      })
    }
  }
}
</script>
